import { ArrayUtil } from '@/util/ArrayUtil'
import { ObjectUtil } from '@/util/ObjectUtil'

/**
 * PeaceRepeatRender 用于指定PeaceColumn渲染个数和保存格式
 */
export default class PeaceRepeatRender {
  /**
   * 保存格式,未配置,则与组件双向绑定的值类型一致
   */
  private _format?: string
  /**
   * 分隔符,默认逗号,当_format == string时有效
   */
  private _split?: string
  /**
   * 渲染个数
   */
  private _num: number
  /**
   * 保存到哪些字段.假设渲染后组件从左到右水平排列,那么最左边的组件保存值所用的字段名使用数组第1个元素值,左边起第二个组件则使用数组第二个元素值,其它以此类推
   * 如果未设置,则在 PeaceColumn 中找到并使用"同级"中的key值
   */
  private _fieldNames?: string[]
  /**
   * [内部使用]
   * 未定义_fieldNames,有num值,生成 _markFieldNames
   */
  private _markFieldNames?: string[]

  constructor(format?: string, split?: string, num?: number,fieldNames: string[]) {
    if (format) {
      if (!PeaceRepeatRender.isValidFormat(format)) {
        throw new Error('无效的格式类型')
      }
      this._format = format
    }else{
      this._format = PeaceRepeatRender.STRING
    }
    this._split = split ?? ','
    this._num = num
    this._fieldNames = fieldNames
  }

  // 静态只读属性
  static readonly STRING: string = 'string'
  static readonly ARRAY: string = 'array'
  static readonly FIELD_NAMES: string = 'fieldNames'

  /**
   * 获取所有可用的格式
   */
  static getAllFormats(): string[] {
    return [
      PeaceRepeatRender.STRING,
      PeaceRepeatRender.ARRAY,
      PeaceRepeatRender.FIELD_NAMES
    ]
  }

  /**
   * 验证格式是否有效
   * @param format 格式
   */
  static isValidFormat(format: string): boolean {
    return PeaceRepeatRender.getAllFormats().includes(format)
  }

  initMarkFileNames(peaceColumnKey?:string): void {
    if (ObjectUtil.isNullOrUndefined(this._num)){
      throw new Error("未定义num值")
    }
    if (ArrayUtil.isNotEmpty(this._fieldNames)){
      return
    }
    let prefix = peaceColumnKey
    if (ObjectUtil.isNullOrUndefined(peaceColumnKey)){
      prefix = new Date().getTime()
    }
    const nameArray: string[] = []
    for (let i = 0; i < this._num; i++) {
      nameArray.push(prefix+""+i)
    }
    this._markFieldNames = nameArray
    this.validate()
  }

  validate(): void {
    let numLength = 0
    if (ArrayUtil.isNotEmpty(this._fieldNames)) {
      numLength = this.fieldNames.length
    }
    if (ArrayUtil.isNotEmpty(this.markFieldNames)) {
      numLength = this.markFieldNames.length
    }
    if (this._num !== numLength) {
      throw new Error("字段名数量和num不匹配")
    }
  }

  returnFileNames(index:number): string {
    if (ArrayUtil.allEmpty(this._fieldNames,this._markFieldNames)) {
      return []
    }
    if (ArrayUtil.isNotEmpty(this._fieldNames)) {
      return this._fieldNames[index]
    }
    return this._markFieldNames[index]
  }

  get format(): string {
    return this._format ?? PeaceRepeatRender.STRING
  }

  set format(value: string) {
    if (!PeaceRepeatRender.isValidFormat(value)) {
      throw new Error('无效的格式类型')
    }
    this._format = value
  }
   get split(): string {
    return this._split
  }

  set split(value: string) {
    this._split = value
  }
  get num(): number {
    return this._num
  }

  set num(value: number) {
    this._num = value
  }
  get fieldNames(): string[] {
    return this._fieldNames
  }

  set fieldNames(value: string[]) {
    this._fieldNames = value
  }
  get markFieldNames(): string[] {
    return this._markFieldNames
  }
  set markFieldNames(value: string[]) {
    this._markFieldNames = value
  }
}
